package org.zxy.org.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zxy.base.annotation.RonghuaPermission;
import org.zxy.base.util.AjaxResult;
import org.zxy.org.domain.Department;
import org.zxy.org.query.DepartmentQuery;
import org.zxy.org.service.IDepartmentService;

import java.util.List;


@RestController
@RequestMapping("/dep")
@RonghuaPermission(name = "部门管理")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //添加和修改接口
    @PutMapping
    @RonghuaPermission(name="部门的添加和修改")
    public AjaxResult saveOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //根据Id查询
    @GetMapping("/{id}")
    @RonghuaPermission(name="根据Id查询部门")
    public AjaxResult selectById(@PathVariable("id") Long id) {
        try {
            System.out.println(departmentService.selectById(id));
            return AjaxResult.success(departmentService.selectById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //查询全部
    @GetMapping
    @RonghuaPermission(name="查询所有部门")
    public AjaxResult selectAll() {
        try {
            return AjaxResult.success(departmentService.selectAll());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //根据ID删除
    @DeleteMapping("/{id}")
    @RonghuaPermission(name="根据ID删除")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        try {
            departmentService.deleteById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping
    @RonghuaPermission(name="部门分页查询")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery) {
        try {
            return AjaxResult.success(departmentService.queryPage(departmentQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @GetMapping("/tree")
    @RonghuaPermission(name="查询部门树")
    public AjaxResult queryTree() {
        try {
            return AjaxResult.success(departmentService.queryDeptTree());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @PatchMapping
    @RonghuaPermission(name="部门批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        try {
            departmentService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }
}
